package com.swpu.service_archive.controller;


import com.swpu.commonutils.JwtUtils;
import com.swpu.commonutils.R;
import com.swpu.commonutils.ResultEncryption;
import com.swpu.service_archive.entity.VaccineRecord;
import com.swpu.service_archive.service.VaccineRecordService;
import com.swpu.service_user.aop.Decrypt;
import com.swpu.service_user.aop.Encrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chaichai
 * @since 2024-03-17
 */
@RestController
@RequestMapping("/service_archive/vaccine-record")
public class VaccineRecordController {
        @Autowired
    private VaccineRecordService vaccineRecordService;
        @PostMapping("/saveUserVaccineData")
        @Decrypt
        @Encrypt
    public R saveUserVaccineData(@RequestBody ResultEncryption resultEncryption, VaccineRecord vaccineRecord, HttpServletRequest request){
            String openid = JwtUtils.getOpenidByJwtToken(request);
            if (openid.isEmpty()){
                return R.error().message("用户未登录");
            }
            vaccineRecordService.saveUserVaccineData(openid,vaccineRecord);
            return R.ok();
        }
    @PostMapping("/updateUserVaccineData")
    @Decrypt
    @Encrypt
    public R updateUserVaccineData(@RequestBody ResultEncryption resultEncryption, VaccineRecord vaccineRecord,HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("用户未登录");
        }
        vaccineRecordService.updateUserVaccineData(vaccineRecord);
        return R.ok();
    }
    @GetMapping("getVaccineDataById/{id}")
    public R getVaccineDataById(HttpServletRequest request,@PathVariable String id){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("用户未登录");
        }
        VaccineRecord vaccineRecord = vaccineRecordService.getVaccineDataById(id);
        return R.ok().data("vaccineRecord",vaccineRecord);
    }
    @GetMapping("getUserVaccineList")
    public R getUserVaccineList(HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("用户未登录");
        }
        List<VaccineRecord> vaccineList = vaccineRecordService.getUserVaccineList(openid);
        return R.ok().data("vaccineList",vaccineList);
    }
    @GetMapping("getVaccineRecordCount")
    public R getVaccineRecordCount(HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("用户未登录");
        }
        int vaccineRecordCount = vaccineRecordService.getVaccineRecordCount(openid);
        return R.ok().data("vaccineRecordCount",vaccineRecordCount);
    }
    @DeleteMapping("deleteVaccineById/{id}")
    public R deleteVaccineById(HttpServletRequest request,@PathVariable String id){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("用户未登录");
        }
        vaccineRecordService.deleteVaccineById(id);
        return R.ok();
    }


}

